#include "stdint.h"
#include "gray_scale_app.h"
#include "autocar_global.h"
#include "./gray/gray.h"

/********************静态变量定义********************/
static uint16_t gray_scale_raw_data[GRAY_SCAN_ADJUST_NUM]; /*原始数据*/
static uint16_t gray_scale_seq = 0; /*灰度扫描次第*/

/********************静态函数声明********************/
static uint16_t majority_vote(uint16_t array[]);

/********************全局函数定义********************/

/**
 * @brief	多状态灰度扫描
 */
void gray_scale_multi_scan(lwq_t* target_que)
{	
	param_event_t send_event;
	uint16_t gray_scale_val;
	
	gray_scan(); /*进行一次灰度扫描*/	
	gray_scale_raw_data[gray_scale_seq] = gray_get_data(); /*读取原始灰度值*/
	gray_scale_seq++; /*次第自增*/
	
	/*当前轮次扫描已经完成*/
	if(gray_scale_seq == GRAY_SCAN_ADJUST_NUM)
	{
		gray_scale_val = majority_vote(gray_scale_raw_data); /*多数表决*/						
		gray_scale_seq = 0; /*清空扫描计数*/
	}
	else
	{
		gray_scale_val = GRAY_UNAVAILABLE; /*未完成扫描，使用无效值*/
	}
	
	if(gray_scale_val == GRAY_UNAVAILABLE)
	{
		return;
	}
	else if(gray_scale_val == 0x0001 || gray_scale_val == 0x0003)
	{	
		/*
			00000 00 00001
			00000 00 00011
		*/
		send_event.event = GRAY_SCALE_RIGHT_5;
		send_event.data = NULL;
		lwq_write(target_que, &send_event); /*发送事件*/
	}
	else if(gray_scale_val == 0x0002 || gray_scale_val == 0x0006)
	{
		/*
			00000 00 00010
			00000 00 00110
		*/
		send_event.event = GRAY_SCALE_RIGHT_4;
		send_event.data = NULL;
		lwq_write(target_que, &send_event); /*发送事件*/
	}
	else if(gray_scale_val == 0x0004 || gray_scale_val == 0x000C)
	{
		/*
			00000 00 00100
			00000 00 01100
		*/
		send_event.event = GRAY_SCALE_RIGHT_3;
		send_event.data = NULL;
		lwq_write(target_que, &send_event); /*发送事件*/
	}
	else if(gray_scale_val == 0x0008 || gray_scale_val == 0x0018)
	{
		/*
			00000 00 01000
			00000 00 11000
		*/
		send_event.event = GRAY_SCALE_RIGHT_2;
		send_event.data = NULL;
		lwq_write(target_que, &send_event); /*发送事件*/
	}
	else if(gray_scale_val == 0x0010 || gray_scale_val == 0x0030)
	{
		/*
			00000 00 10000
			00000 01 10000
		*/
		send_event.event = GRAY_SCALE_RIGHT_1;
		send_event.data = NULL;
		lwq_write(target_que, &send_event); /*发送事件*/
	}
	else if(gray_scale_val == 0x0060 || gray_scale_val == 0x0040 || gray_scale_val == 0x0020)
	{
		/*
			00000 11 00000
			00000 10 00000
			00000 01 00000
		*/
		send_event.event = GRAY_SCALE_CENTER;
		send_event.data = NULL;
		lwq_write(target_que, &send_event); /*发送事件*/
	}
	else if(gray_scale_val == 0x00C0 || gray_scale_val == 0x0080)
	{
		/*
			00001 10 00000
			00001 00 00000			
		*/
		send_event.event = GRAY_SCALE_LEFT_1;
		send_event.data = NULL;
		lwq_write(target_que, &send_event); /*发送事件*/
	}
	else if(gray_scale_val == 0x0180 || gray_scale_val == 0x0100)
	{
		/*
			00011 00 00000
			00010 00 00000
		*/
		send_event.event = GRAY_SCALE_LEFT_2;
		send_event.data = NULL;
		lwq_write(target_que, &send_event); /*发送事件*/
	}
	else if(gray_scale_val == 0x0300 || gray_scale_val == 0x0200)
	{
		/*
			00110 00 00000
			00100 00 00000
		*/
		send_event.event = GRAY_SCALE_LEFT_3;
		send_event.data = NULL;
		lwq_write(target_que, &send_event); /*发送事件*/
	}
	else if(gray_scale_val == 0x0600 || gray_scale_val == 0x0400)
	{
		/*
			01100 00 00000
			01000 00 00000
		*/
		send_event.event = GRAY_SCALE_LEFT_4;
		send_event.data = NULL;
		lwq_write(target_que, &send_event); /*发送事件*/
	}
	else if(gray_scale_val == 0x0C00 || gray_scale_val == 0x0800)
	{
		/*
			11000 00 00000
			10000 00 00000
		*/
		send_event.event = GRAY_SCALE_LEFT_5;
		send_event.data = NULL;
		lwq_write(target_que, &send_event); /*发送事件*/
	}
	else if(gray_scale_val == 0x0000)
	{
		/*
			00000 00 00000
		*/
		send_event.event = GRAY_SCALE_WHITE;
		send_event.data = NULL;
		lwq_write(target_que, &send_event); /*发送事件*/
	}
	
}

/**
 * @brief	双态灰度扫描
 */
void gray_scale_twostate_scan(lwq_t* target_que)
{
	param_event_t send_event;
	uint16_t gray_scale_val;
	
	gray_scan(); /*进行一次灰度扫描*/	
	gray_scale_raw_data[gray_scale_seq] = gray_get_data(); /*读取原始灰度值*/
	gray_scale_seq++; /*次第自增*/
	
	/*当前轮次扫描已经完成*/
	if(gray_scale_seq == GRAY_SCAN_ADJUST_NUM)
	{
		gray_scale_val = majority_vote(gray_scale_raw_data); /*多数表决*/						
		gray_scale_seq = 0; /*清空扫描计数*/
	}
	else
	{
		gray_scale_val = GRAY_UNAVAILABLE; /*未完成扫描，使用无效值*/
	}
	
	if(gray_scale_val == GRAY_UNAVAILABLE)
	{
		return;
	}
	else if(gray_scale_val == 0x00)
	{
		send_event.event = GRAY_SCALE_WHITE;
		send_event.data = NULL;
		lwq_write(target_que, &send_event); /*发送事件*/
	}
	else
	{
		send_event.event = GRAY_SCALE_BLACK;
		send_event.data = NULL;
		lwq_write(target_que, &send_event); /*发送事件*/
	}
}

/**
 * @brief	清空灰度扫描
 */
void gray_scan_clear(void)
{
	gray_scale_seq = 0;
	
	for(uint16_t i = 0; i < GRAY_SCAN_ADJUST_NUM; i++)
	{
		gray_scale_raw_data[i] = GRAY_UNAVAILABLE;
	}	
}

/********************静态函数定义********************/

/*多数表决*/
static uint16_t majority_vote(uint16_t array[])
{
	for (uint16_t i = 0; i < GRAY_SCAN_ADJUST_NUM; i++) 
	{
        uint16_t count = 0;
        for (uint16_t j = 0; j < GRAY_SCAN_ADJUST_NUM; j++) 
		{
            if (array[i] == array[j]) 
			{
                count++;
            }
        }
        if (count >= GRAY_SCAN_ADJUST_THRE)
		{
            return array[i];
        }
    }
    return GRAY_UNAVAILABLE;
}
